{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bb4d3131",
   "metadata": {},
   "source": [
    "---------------\n",
    "\n",
    "*本学习记录人：罗林胜   \n",
    "*学习时间：2021-11-18  \n",
    "*week07  \n",
    "*[参考的文档](https://www.python.org/doc/)  \n",
    "*[许老师博客](https://www.cnblogs.com/xuzhichao)  \n",
    "\n",
    "----------------\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1804b0fc",
   "metadata": {},
   "source": [
    "# 回顾上节课内容\n",
    "## 1.认识列表：[value1,value2,value3]\n",
    "## 2.切片进行查询（多值）：\n",
    "> 1.索引值进行查询（单个值）：index  \n",
    "> 2.切片进行查询（多值）slice  \n",
    "##  3.列表的一般方法\n",
    "\n",
    "> 1.增加元素\n",
    ">> 1.append() 增加元素到列表到末尾: L.append(object)\n",
    ">> 2.insert() 指定位置增加元素 : L.insert(index)   \n",
    "\n",
    "> 2.删除元素\n",
    ">> 1.remove() 按照 值 进行删除 : L.remove(value)  \n",
    ">> 2.pop()按照 索引值 进行删除： L.pop(index) 注意：不写index 默认删除列表的最后一个元素  \n",
    "\n",
    ">3.修改内容  \n",
    "\n",
    "> 4.查询内容（参考上述2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f440532c",
   "metadata": {},
   "source": [
    "## 本周内容：\n",
    "### 2.1列表的其他方法\n",
    "### 2.2列表的实践（课本）\n",
    "### 2.3列表的实践（课外）\n",
    "\n",
    "# 3 列表的其他方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "5422042b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Mike', 'Mary', 'Jan', 'Jack']\n"
     ]
    }
   ],
   "source": [
    "names = [\"Mike\",\"Mary\",\"Jan\",\"Jack\"]\n",
    "\n",
    "print(names)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "87a3ebbe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function clear:\n",
      "\n",
      "clear() method of builtins.list instance\n",
      "    Remove all items from list.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(names.clear)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cc7eeed",
   "metadata": {},
   "source": [
    ">1.clear()  \n",
    ">>1.使用方法：L.clear()  \n",
    ">>2.结果：remove all items from L  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "25bfbb54",
   "metadata": {},
   "outputs": [],
   "source": [
    "names.clear()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "88308195",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "names"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74e946b9",
   "metadata": {},
   "source": [
    "> 2.count()  \n",
    ">> 1.使用方法：L.count(value)  \n",
    ">> 2.结果：查询某个value出现的次数（共现的次数），通常用在计算词频，统计操作当中  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "68dbfc7c",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name '分数_list' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-bd931e5918b2>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhelp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m分数_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcount\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name '分数_list' is not defined"
     ]
    }
   ],
   "source": [
    "help(分数_list.count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8ce39742",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 89, 68, 78, 88, 90, 93, 88]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list = [88,89,68,78,88,90,93,88]\n",
    "分数_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2c8c156d",
   "metadata": {},
   "source": [
    "> 3.extend()扩展\n",
    ">> 1.使用方法：L.extend(iterable)  \n",
    ">> 2.结果：iterable 将拼接在 L 的后面（末尾）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "27f5bb2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数字媒体技术1班=[88,78,96,89,99,98,78]\n",
    "分数_list_数字媒体技术2班=[88,88,96,79,97,98,78]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "63091051",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function extend:\n",
      "\n",
      "extend(iterable, /) method of builtins.list instance\n",
      "    Extend list by appending elements from the iterable.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(分数_list.extend)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "665790bb",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数字媒体技术1班.extend(分数_list_数字媒体技术2班)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cb71c81a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 78, 96, 89, 99, 98, 78, 88, 88, 96, 79, 97, 98, 78]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "分数_list_数字媒体技术1班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "b4a43ee6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[88, 78, 96, 89, 99, 98, 78, 88, 88, 96, 79, 97, 98, 78]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#等价于\n",
    "[88,78,96,89,99,98,78]+[88,88,96,79,97,98,78]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4d08929",
   "metadata": {},
   "source": [
    "> 4. * copy()\n",
    ">> 1. 在列表中赋值不等价于复制\n",
    ">> 2.真正的复制列表的两种常见方法\n",
    ">>>1.copy()\n",
    ">>>2.切片[:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "b26df695",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "jack alex\n",
      "1782936780336 1782936746672\n"
     ]
    }
   ],
   "source": [
    "name1=\"alex\"\n",
    "\n",
    "name2=name1\n",
    "\n",
    "name1=\"jack\"\n",
    "\n",
    "print(name1,name2)\n",
    "print(id(name1),id(name2))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10bbd3d0",
   "metadata": {},
   "source": [
    "结论是；赋值有两种情况：\n",
    "> 1.赋值的数据类型是 不可变的数据 (字符串、数值、元组)，分配不同的数据地址  \n",
    "> 2.赋值的数据类型是 可变的数据（列表）分配同一个地址，指向同一个列表  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "ce0521bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 此案例来源，课本p72：看起来像复制，但其实不是\n",
    "first=[1,2,3,4,5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "31b674ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "92d085cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2135508164096\n"
     ]
    }
   ],
   "source": [
    "print(id(first))  # first 的 数据地址： 140454682100488"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "ef5ba01b",
   "metadata": {},
   "outputs": [],
   "source": [
    "second=first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "d20f35aa",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2135508164096\n"
     ]
    }
   ],
   "source": [
    "print(id(second)) # second 的 数据地址： 140454682100488"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0643d20a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4, 5]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "81fb9a74",
   "metadata": {},
   "outputs": [],
   "source": [
    "first.append(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c0fd5eb9",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 6] [1, 2, 3, 4, 5, 6, 6]\n"
     ]
    }
   ],
   "source": [
    "print(first,second)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "659cef82",
   "metadata": {},
   "source": [
    "#  列表的复制（方法一）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "903673e9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]\n",
      "1782936800064 1782936799808\n"
     ]
    }
   ],
   "source": [
    "first=[1,2,3,4,5]\n",
    "second=first.copy()\n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "cee13363",
   "metadata": {},
   "outputs": [],
   "source": [
    "first.append(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "359d2487",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 6] [1, 2, 3, 4, 5]\n"
     ]
    }
   ],
   "source": [
    "print(first,second)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "91c8e055",
   "metadata": {},
   "source": [
    "#  思考 copy() 只复制 第一层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "05d90bd7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, [4, 5, 6]] [1, 2, 3, [4, 5, 6]]\n",
      "2135507373568 2135507374848\n"
     ]
    }
   ],
   "source": [
    "first = [1,2,3,[4,5,6]]\n",
    "second = first.copy()\n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d3c69d50",
   "metadata": {},
   "outputs": [],
   "source": [
    "first[3].append(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "93c73eca",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [4, 5, 6, 7, 7]]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "79924af7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, [4, 5, 6, 7, 7]]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "second"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "74df6c82",
   "metadata": {},
   "source": [
    "> 6.sort()\n",
    ">>1. reverse=False (默认从小到大)\n",
    ">>2. 可修改 reverse = True (从大到小)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "f8451554",
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name '分数_list_数媒技术1班' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-21-a924589bd6b7>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mhelp\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m分数_list_数媒技术1班\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name '分数_list_数媒技术1班' is not defined"
     ]
    }
   ],
   "source": [
    "help(分数_list_数媒技术1班.sort)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "c9920616",
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name '分数_list_数媒技术1班' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-22-9dae611059f0>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0m分数_list_数媒技术1班\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msort\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m: name '分数_list_数媒技术1班' is not defined"
     ]
    }
   ],
   "source": [
    "分数_list_数媒技术1班.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2595a26",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数媒技术1班"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "17719e75",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数媒技术1班.sort(reverse = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ea8a2109",
   "metadata": {},
   "outputs": [],
   "source": [
    "分数_list_数媒技术1班"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ef75bcf",
   "metadata": {},
   "source": [
    "#  列表的复制（方法二）\n",
    "* 切片[:] 可以实现真正意义的复制"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "6e6bef2e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5] [1, 2, 3, 4, 5]\n",
      "2135507376256 2135507588864\n"
     ]
    }
   ],
   "source": [
    "first = [1,2,3,4,5]\n",
    "second = first[:] \n",
    "\n",
    "print(first,second)\n",
    "print(id(first),id(second))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0efa0107",
   "metadata": {},
   "source": [
    "## 列表的实践（课本方案1）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "5ebb94fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', \"'\", 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'p', 'a']\n",
      "['o', 'n', 't', ' ', 'a', 'p']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "# 目标：\"Dont' panic\"=>\"on top\"\n",
    "# 利用列表的 方法 实现\n",
    "phrase=\"Don't panic!\"\n",
    "\n",
    "plist=list(phrase)  # list()：str--->list\n",
    "print(plist)\n",
    "\n",
    "# 删除D\n",
    "plist.remove('D')\n",
    "print(plist)\n",
    "\n",
    "# 删除末尾4个元素  用pop（）删除4次\n",
    "\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "print(plist)\n",
    "\n",
    "#删除‘’‘\n",
    "plist.pop(2)\n",
    "print(plist)\n",
    "#plist extend([plist.pop(),plist.pop()])# 难点在于；执行的运算先后顺序 先算圆括号，\n",
    "#['o','n','t','']  +  ['a'    ,      'p'   ]\n",
    "plist.insert(4,plist.pop())\n",
    "print(plist)\n",
    "plist.insert(2,plist.pop(3))\n",
    "#['o','n','t','a','p']在第二个位置增加’‘\n",
    "print(plist)\n",
    "new_phrase=''.join(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f97c4252",
   "metadata": {},
   "source": [
    "## 列表的实践（课本方案2）\n",
    "* 对比 列表的实践一 结论：\n",
    "> 1. 列表的方法 会修改 原本的列表  但是 **列表的切片** 不改变原列表\n",
    "> 2.使用列表方法时，需考虑是否需要先复制列表\n",
    "> 3.思考：如何对比代码执行效率？ 代码执行后的时间-代码执行前的时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "ecea9302",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "['o', 'n']"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 目标：\"Dont' panic\"=>\"on top\"\n",
    "# 利用列表的 切片 实现\n",
    "实践二运行前的时间=time.time()\n",
    "phrase=\"Don't panic!\"\n",
    "\n",
    "plist=list(phrase)  # list()：str--->list\n",
    "print(plist)\n",
    "\n",
    "new_phrase=''.join(plist[5]+plist[4]+plist[7]+plist[6])\n",
    "\n",
    "plist[1:3].extend"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "4943bf1f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "' tap'"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist[5]+plist[4]+plist[7]+plist[6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "b92d2586",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'t'"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "plist[4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6d7aefc",
   "metadata": {},
   "outputs": [],
   "source": [
    "list(plist[5])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
